﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;
using PeoplesJukebox.Web.Entities;
using PeoplesJukebox.Web.Enums;

namespace PeoplesJukebox.Web.Pages
{
    /// <summary>
    /// Page showing a filterable list of artists
    /// </summary>
    public partial class ListPage : PageBase
    {
        private bool _suspendReload = false;

        /// <summary>
        /// Returns the page type
        /// </summary>
        public override PageType PageType
        {
            get { return PageType.List; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public ListPage()
        {
            InitializeComponent();
            this.CatalogueServiceClient.SearchComplete += new PeoplesJukebox.Web.ServiceClients.CatalogueServiceClient.SearchCompleteEventHandler(CatalogueServiceClient_SearchComplete);
            this.resultsList.TrackSelected += new PeoplesJukebox.Web.UserControls.ResultsList.OnTrackSelectedEventHandler(resultsList_TrackSelected);
            this.resultsList.ArtistSelected += new PeoplesJukebox.Web.UserControls.ResultsList.OnArtistSelectedEventHandler(resultsList_ArtistSelected);
            this.keyPad.KeypadCleared += new PeoplesJukebox.Web.UserControls.KeyPad.KeypadClearedEventHandler(keyPad_KeypadCleared);
            this.keyPad.TextBox = this.txtSearchTerm;
        }

        /// <summary>
        /// Resets page state
        /// </summary>
        public override void Reset()
        {
            base.Reset();
            this._suspendReload = true;
            this.txtSearchTerm.Text = string.Empty;
            this.Load();
        }

        private void resultsList_ArtistSelected(MediaItem artist)
        {
            this._suspendReload = true;
            this.txtSearchTerm.Text = artist.Name;
        }

        private void keyPad_KeypadCleared()
        {
            this.Reset();
        }

        private void resultsList_TrackSelected(MediaItem track)
        {
            this.DataContext = track;
            this.Completed(PageType.ConfirmTrackSelection);
        }

        private void CatalogueServiceClient_SearchComplete(IEnumerable results)
        {
            this.resultsList.Results = results;
            this.txtSearchTerm.Visibility = this.keyPad.Visibility = Visibility.Visible;
        }

        private void txtSearchTerm_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!_suspendReload)
            {
                this.Load();
            }
            _suspendReload = false;
        }

        private void Load()
        {
            this.resultsList.Clear();
            if (string.IsNullOrEmpty(this.txtSearchTerm.Text))
            {
                this.CatalogueServiceClient.GetArtists();
            }
            else
            {
                this.CatalogueServiceClient.SearchArtists(this.txtSearchTerm.Text);
            }
        }
    }
}